<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field.options.displayStyle"
  >
    <div class="scene-report-info-widget" :style="getStyle('viewArea')">
      <div class="widget-inside">
        <div class="top-area">
          <div class="top-area-inside">
            <div class="logo items">
              <template v-if="!!optionsInfo?.logoInfo?.value?.url">
                <img :src="pictureResource" alt="企业Logo"/>
              </template>
            </div>
            <div
                class="descriptor items"
                :style="getStyle('descriptorInfo')"
                v-if="optionsInfo.descriptorInfo.shown">
              {{ optionsInfo.descriptorInfo.value }}
            </div>
          </div>
        </div>
        <div class="keyboard-area">
          <template v-if="showComp(0)">
            <div class="keyboard-info">
              <!--keyboardType=> 1:身份证 2:非身份证-->
              <reportKeyboard2 :field="field" @handleSubmit="handleSubmit" ref="keyboard" :devType="devType" :keyboardType="keyboardTypeInfo"/>
            </div>
            <div class="tips-info" :style="getStyle('tips')">
              <div class="left" :style="getStyle('tipsSign')">
                <Icon name="volume"/>
              </div>
              <div class="right" :style="getStyle('tipsContent')" v-html="optionsInfo['tipContent']"/>
            </div>
          </template>
          <template v-if="showComp(1)">
            <ReportResult3
                :state="1"
                :field="field"
                :layerNameKeySet="optionsInfo.layerNameKeySet"
                :devType="devType"
                @handleReport="handleReport"
                @handleReportAll="handleReportAll"
                @handleSignPrint="handleSignPrint"
            />
          </template>
          <!--底部操作按钮-->
          <div class="operate-info" v-if="getReportStepInfo===1">
            <div class="back-home" @click="handleBack(true)">
              <Icon name="wap-home"/>
              <span class="back-text">
                返回首页({{ backcountdown / 1000 }}s)
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {Icon} from 'vant';
import {v4 as uuid} from 'uuid';
import {pxToVw} from "@/utils/common";
import emitter from "@/utils/designer/emitter";
import {buildParentApi} from "@/utils/designer/util";
import ScrollX from "@c/public/seamlessScroll/ScrollX.vue";
import reportKeyboard2 from "./reportComps/reportKeyboard2.vue";
import ReportResult3 from './reportComps/reportResult3.vue';
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import ReportResult
  from "@sc/templateDetails/modules/homeMiddle/scene-widget/module/report/reportComps/reportResult.vue";
import * as types from "@c/public/form-designer/devType/datas-types";

export default {
  name: "scene-report-keyboard2-info-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    ReportResult,
    Icon,
    ScrollX,
    CompOperate,
    ReportResult3,
    reportKeyboard2,
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
    devType: {
      type: Boolean,
      default: false,
    },
  },

  data() {
    return {
      number: 0,
      keyboardType:2,
      widgetKey: uuid(),
      backcountdown: 120000,
      optionsInfo: Object(),
    }
  },

  computed: {
    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Object();
        let Obj = Object();
        switch (val) {
          case 'viewArea':
            Reflect.set(Obj, 'height', pxToVw(info.viewHeight));
            Reflect.set(Obj, 'backgroundColor', `${(info.backgroundColor)}`);
            return Obj;
          case 'descriptorInfo':
            let {fontSize = 90, fontColor = "rgba(255,255,255,1)", fontWeight = 'normal'} = info?.descriptorInfo || {}
            Reflect.set(Obj, "color", (fontColor));
            Reflect.set(Obj, "fontSize", pxToVw(fontSize));
            Reflect.set(Obj, "fontWeight", fontWeight);
            return Obj;
          case "tips":
            Reflect.set(Obj, "height", pxToVw(info.tipsAreaHeight));
            Reflect.set(Obj, "color", (info.tipsFontColor));
            Reflect.set(Obj, "fontSize", pxToVw(info.tipsFontSize));
            return Obj;
          case "tipsSign":
            Reflect.set(Obj, "color", (info.tipsFontColor));
            return Obj;
          case "tipsContent":
            Reflect.set(Obj, "fontSize", pxToVw(info.tipsContentFontSize));
            Reflect.set(Obj, "color", (info.tipsContentFontColor));
            return Obj;
        }
      }
    },

    //获取LOGO资源
    pictureResource() {
      let {logoInfo} = this.optionsInfo;
      if ((logoInfo?.value) && this.$lodash.isObject(logoInfo.value)) {
        return logoInfo.value?.url;
      } else {
        return imageUrl;
      }
    },

    //步距展示对应的组件
    showComp() {
      return function (val) {
        let {step = 0} = this.optionsInfo
        return step === val
      }
    },

    getReportStepInfo() {
      let {step = 0} = this.optionsInfo;
      return parseInt(step)
    },

    keyboardTypeInfo(){
      let {keyboardType = 1} = this.optionsInfo;
      return keyboardType
    }


  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
    this.$store.dispatch('setReportStepInfo', 0)
  },

  methods: {

    // 提交号别
    handleSubmit(params) {
      this.parentApi.executeParentMethod('handleReport', params);
    },

    // 单独报到
    handleReport(params) {
      console.log('单独报到');
      this.parentApi.executeParentMethod('toReport', params);
    },

    // 批量报到
    handleReportAll(params) {
      console.log('批量报到-----');
      this.parentApi.executeParentMethod('reportAll', params);
    },

    handleSignPrint(params) {
      console.log('info-打印小票');
      this.parentApi.executeParentMethod('signPrint', params);
    },

    //允许报到列表
    allowReportLen() {
      let devType = (this.devType);
      let reportPatientInfo = this.$store.getters['getReportPatientInfo'];
      let patientInfos = devType ? (reportPatientInfo.apiData) : (reportPatientInfo.mockData)
      return patientInfos?.unSignList?.length;
    },

    // 接收父组件传递过来的数据
    async executeMethod(key, resp) {
      switch (key) {
        case "reportPatientInfo":
          let {code, data} = resp;
          if (code === 200) {
            let dataInfo = this.$lodash.cloneDeep(data);
            await this.$store.dispatch('setReportPatientInfo', dataInfo);
            Reflect.set(this.optionsInfo, 'step', 1);
          }
          break;
        case "reportBack":
          console.log('reportBack');
          Reflect.set(this.optionsInfo, 'step', 0)
          break
      }
    },

    // 返回首页
    handleBack(info = false) {
      Reflect.set((this.optionsInfo), 'step', 0);
      this.parentApi.executeParentMethod('openReadCardScan');
    }
  },

  watch: {

    // 步距
    getReportStepInfo: {
      handler: function (val) {
        const self = this;
        this.optionsInfo.step = parseInt(val);
        // 步骤过程发生变化--执行时间的倒计时
        if (val === 1) {
          self.timer = setInterval(() => {
            self.backcountdown = (self.backcountdown - 1000);
            // 到达时间--返回首页-清空定时器
            if ((self.backcountdown) <= 0) {
              self.handleBack(true)
              clearInterval(self.timer)
            }
          }, 1000)
        } else {
          clearInterval(self.timer)
        }
      },
      deep: true,
      immediate: true,
    },

    // 监听父级传递过来的props--field
    field: {
      handler: function (val) {
        let key = val.options['config'];
        if (Reflect.has(val.options[key], 'patientShowFields')) {
          Reflect.set(val.options[key], 'patientShowFields', types['patientFields'])
        }
        this.optionsInfo = val.options[key];
        this.backcountdown = (this.optionsInfo.backCountdown) * 1000 || 120000;
        this.$store.dispatch('setReportStepInfo', (this.optionsInfo.step));
      },
      deep: true,
      immediate: true,
    },

  },

  destroyed() {
    this.unregisterFromRefList();
    this.$store.dispatch('setReportStepInfo', 0);
  },

};
</script>

<style lang="scss" scoped>
.scene-report-info-widget {
  font-size: 60px;
  width: 100%;
  border-radius: 28px 28px 0 0;
  box-sizing: border-box;
  *{
    user-select: none; /* 标准语法 */
    touch-action: manipulation; /* 阻止默认的双击缩放 */
  }
  //报到首页
  .widget-inside {
    height: calc(100%);
    width: 100%;
    box-sizing: border-box;
    background: transparent !important;

    .top-area {
      width: 100%;
      height: 200px;
      overflow: hidden;
      padding: 0 30px;
      box-sizing: border-box;
      @include background('design/1-21×2');
      display: flex;
      flex-direction: row;
      align-self: center;
      align-items: center;
      align-content: center;
      justify-content: space-between;

      .top-area-inside {
        margin-top: -20px;
        width: 100%;
        padding: 20px 0;
        box-sizing: border-box;
        height: calc(100% - 60px - 2px);
        display: flex;
        flex-direction: row;
        overflow: hidden;
        flex-wrap: nowrap;
        justify-content: space-between;

        .items {
          flex-shrink: 0;
          flex-basis: 0;
          flex-grow: 1;
        }
      }

      .logo {
        min-width: auto;
        flex-grow: 2 !important;


        img {
          height: 100% !important;
          max-height: 90px !important;
          max-width: 100% !important;
          object-fit: contain;
        }
      }

      .descriptor {
        font-size: 120px;
        color: #FFFFFF;
        text-align: right;
        @include singleEllipsis(100%);
        width: auto;
        flex-grow: 3 !important;
      }
    }

    .keyboard-area {
      width: 100%;
      background: #E9F4FF !important;
      overflow: hidden;
      margin-top: -40px;
      border-radius: 32px 32px 0 0;
      height: calc(100% - 200px + 40px);
      position: relative;
      display: flex;
      flex-direction: column;

      .keyboard-info {
        margin: 34px 34px;
        box-sizing: border-box;
        background: white;
        border-radius: 24px;
        width: calc(100% - 68px);
        overflow: hidden;
        flex-grow: 1;
      }

      .tips-info {
        width: 100%;
        border-radius: 24px 24px 0 0;
        height: 144px;
        padding: 0 40px;
        box-sizing: border-box;
        overflow: hidden;
        background: rgba(255, 144, 43, 0.2);
        font-weight: 400;
        font-size: 40px;
        color: #FF902B;
        display: flex;
        align-items: center;
        align-self: center;
        justify-content: left;
        align-content: center;

        .left {
          width: 90px;
          flex-shrink: 0;
          color: #FF902B;
          box-sizing: border-box;
          margin-right: 20px;
        }

        .right {
          box-sizing: border-box;
          font-size: 68px;
          flex-shrink: 0;
          line-height: 1.4;
          max-width: calc(100% - 90px - 30px);
        }

      }

      .operate-info {
        position: absolute;
        left: 50%;
        bottom: 0;
        transform: translate(-50%, 0);

        .back-home {
          align-self: center;
          background: rgba(0, 0, 0, 0.8);
          color: white;
          padding: 24px 52px;
          font-size: 44px;
          border-radius: 24px 24px 0 0;

          .van-icon {
            font-size: 58px;
            padding-right: 20px;
          }
        }
      }
    }
  }
}
</style>
